<?php
/**
 * Consumable.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-19
 * Time: 10:30
 */

namespace App\Http\Controllers\Api\BaseData;


use App\Http\Controllers\Base;
use App\Tables;

class PostPrint extends Base
{

    public function getList()
    {
        $name = $this->request->get('name', '');
        if(!empty($name)) {
            $all = Tables\PostPrint::where(['name' => $name])->orWhere('name', 'like', "%{$name}%")->get();
        } else {
            $all = Tables\PostPrint::all();
        }
        $r = [];
        foreach($all as $item) {
            $r[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'pyCode' => $item['py_code']
            ];
        }
        return $this->responseSuccess($r);
    }

    public function get()
    {
        $id = $this->request->get('id', 0);
        $r = ['id' => 0, 'name' => '', 'pyCode' => ''];
        $find = Tables\PostPrint::where(['id' => $id])->first();
        if(empty($find)) { return $this->responseSuccess($r);}
        $r['id'] = $find['id'];
        $r['name'] = $find['name'];
        $r['pyCode'] = $find['py_code'];
        return $this->responseSuccess($r);
    }

    public function add()
    {
        $name = $this->request->post('name', '');
        $pyCode = $this->request->post('pyCode', '');
        if(empty($name)) {
            return $this->responseFail('名称不能为空！');
        }

        $find = Tables\PostPrint::where(['name' => $name])->first();
        if(!empty($find)) { return $this->responseFail('名称已经存在了！');}

        $ok = (bool) Tables\PostPrint::insertGetId(['name' => $name, 'py_code' => $pyCode, 'add_time' => time()]);
        return $ok ? $this->responseSuccess() : $this->responseFail();
    }

    public function update()
    {
        $id = (int) $this->request->post('id', 0);
        $name = $this->request->post('name', '');
        $pyCode = $this->request->post('pyCode', '');
        if(empty($name)) {
            return $this->responseFail('名称不能为空！');
        }
        if($id <= 0) {
            return $this->responseFail('无效的操作对象！');
        }
        $find = Tables\PostPrint::where(['id' => $id])->first();
        if(empty($find)) {
            return $this->responseFail('未查询到信息！');
        }
        $find->name = $name;
        $find->py_code = $pyCode;
        return (bool) $find->save() ? $this->responseSuccess() : $this->responseFail();
    }

    public function del()
    {
        $id = (int) $this->request->post('id', 0);
        if($id <= 0) {
            return $this->responseFail('无效的操作对象！');
        }

        $oidList = Tables\Order::query()
            ->whereRaw('(make_status!=1 or delivery_status!=1 or pay_status!=1)')
            ->where('dumped', Tables\Order::No_Dumped)
            ->pluck('id');
        if($oidList->isNotEmpty()) {
            $count = Tables\OrderPostPrint::query()
                ->whereIn('oid', $oidList)
                ->where('pid', $id)->count();
            if($count > 0) {
                return $this->responseFail('有正在进行中的订单，无法删除！');
            }
        }


        return (bool) Tables\PostPrint::where(['id' => $id])->delete() ?
            $this->responseSuccess() :
            $this->responseFail('删除失败！');
    }

}